<?php
namespace Custom\Controller;
use Think\Controller;

/**
 * 门店操作
 */
class StoreController extends UserController{
    function __construct()
    {
        parent::__construct();
        $this->M_store = M('Store');
    }

    //门店首页
    public function index()
    {
        $info = $this->M_store->where('c_id='.session('c_id'))->field('s_id,store_name,tel,address,email,store_user,store_time')->select();
        $this->assign('info',$info);
        $this->display();
    }

    //添加门面
    public function addStore()
    {
        if(IS_POST){
            $data['c_id'] = session('c_id');
            $data['store_name'] = trim(I('post.store_name'));
            $data['address'] = trim(I('post.store_address'));
            $data['tel'] = trim(I('post.store_tel'));
            $data['store_user'] = trim(I('post.store_user'));
            $data['store_time'] = trim(I('post.store_time'));
            $data['email'] = trim(I('post.store_email'));
            $data['wechat_public'] = trim(I('post.store_public'));
            $data['address_lng'] = trim(I('post.address_lng'));//经度
            $data['address_lat'] = trim(I('post.address_lat'));//纬度
            $data['store_intro'] = trim(I('intro'));
            $data1['appid'] = I('post.appid');
            $data1['template_id']  = I('post.template_id');
            $data1['secret'] = I('post.secret');
            $data1['mch_id'] = I('post.mch_id');
            
            $data1['app_key']  = I('post.app_key');
            
            $upload = new \Think\Upload();
            $upload->maxSize   =     3145728 ;// 设置附件上传大小，默认3M
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =     './'; // 设置附件上传根目录
            $upload->savePath  =     './Uploads/data/store_thumb/'; // 设置附件上传（子）目录// 上传文件
            $img_info   =   $upload->upload();
            if(!$img_info) {// 上传错误提示错误信息
                $this->error($upload->getError());
            }else{// 上传成功
                $this->M_store->thumb = $img_info['cate_thumb']['savepath'].$img_info['cate_thumb']['savename'];
            }
            $data['thumb'] = $img_info['cate_thumb']['savepath'].$img_info['cate_thumb']['savename'];
            $res = $this->M_store->add( $data );
            $data1['s_id'] = $res;
            $res1 = M('store_conf')->add( $data1 );
            if($res || $res1){
                $this->success(L('adsuccess'),U('Store/index'));
            }else{
                $this->error(L('failed'));
            }
        }
        $this->display();
    }



    //删除门店
    public function delStore()
    {
        $id = I('get.id',0,'intval');
        $res = $this->M_store->delete($id);
        if($res){
            $this->ajaxReturn(array('result'=>'tp1'));
        }else{
            $this->ajaxReturn(array('result'=>'break'));
        }
    }

    //编辑门面
    public function editStore()
    {
        $id = I('get.id',0,'intval');
        $info = $this->M_store->join('as a left join sp_store_conf as b on a.s_id=b.s_id')->where('a.s_id='.$id)->select();

        $this->assign('info',$info);
        if(IS_POST){
            $this->_edit();
        }
        $this->display();
    }

    public function _edit()
    {
        $data['s_id'] = I('post.id',0,'intval');
        $data['store_name'] = trim(I('post.store_name'));
        $data['address'] = trim(I('post.store_address'));
        $data['tel'] = trim(I('post.store_tel'));
        $data['store_user'] = trim(I('post.store_user'));
        $data['store_time'] = trim(I('post.store_time'));
        $data['email'] = trim(I('post.store_email'));
        $data['wechat_public'] = trim(I('post.store_public'));
        $data1['conf_id'] = I('post.conf_id');
        $data1['appid'] = I('post.appid');
        $data1['secret'] = I('post.secret');
        $data1['mch_id'] = I('post.mch_id');
        $data1['app_key']  = I('post.app_key');
        $data1['template_id']  = I('post.template_id');
        $data1['s_id'] = I('post.id',0,'intval');
        $res = $this->M_store->save( $data );
        $res1 = M('store_conf')->save($data1);
        if($res || $res1){
            $this->success(L('adsuccess'),U('Store/index'));
        }else{
            $this->error(L('failed'));
        }
    }

    /*
     * 选择门店
     */
    public function selStore()
    {
        $info = $this->M_store->field('s_id,store_name')->select();
        $this->assign('info',$info);
        if(IS_POST){
            $s_id = I('post.store_id');
            session('s_id',$s_id);
            die($this->success('设置成功',U('goods/index')));
        }
        $this->display();
    }
}